/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tools.idea.wear.dwf.dom.raw

import com.android.tools.idea.wear.dwf.dom.raw.expressions.WFFExpressionLiteralExpr
import com.android.tools.wear.wff.WFFVersion
import com.intellij.lang.injection.InjectedLanguageManager
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.psi.util.parentOfType
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import com.intellij.testFramework.replaceService
import org.mockito.Mockito.mock
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever

fun manifestWithWFFVersion(version: String) =
  // language=XML
  """
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.tools.idea.wear.dwf.dom.raw">
    <uses-feature android:name="android.hardware.type.watch" />
    <application
        android:icon="@drawable/preview"
        android:label="@string/app_name"
        android:hasCode="false"
        >

        <meta-data
            android:name="com.google.android.wearable.standalone"
            android:value="true" />

        <property
            android:name="com.google.wear.watchface.format.version"
            android:value="$version" />
        <property
            android:name="com.google.wear.watchface.format.publisher"
            android:value="Test publisher" />

        <uses-library
            android:name="com.google.android.wearable"
            android:required="false" />
    </application>
</manifest>
        """
    .trimIndent()

/** Overrides the [WFFVersion] returned by [CurrentWFFVersionService]. */
fun overrideCurrentWFFVersion(wffVersion: WFFVersion?, disposable: Disposable) {
  val mockCurrentWFFVersionService = mock<CurrentWFFVersionService>()
  whenever(mockCurrentWFFVersionService.getCurrentWFFVersion(any()))
    .thenReturn(wffVersion?.let { CurrentWFFVersion(wffVersion, isFallback = false) })
  ApplicationManager.getApplication()
    .replaceService(CurrentWFFVersionService::class.java, mockCurrentWFFVersionService, disposable)
}

/** Finds the injected [WFFExpressionLiteralExpr] at the caret offset. */
fun JavaCodeInsightTestFixture.findInjectedExpressionLiteralAtCaret() =
  InjectedLanguageManager.getInstance(project)
    .findInjectedElementAt(file, caretOffset)
    ?.parentOfType<WFFExpressionLiteralExpr>(withSelf = true)
